package com.jxc.dao.impl;

import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.jxc.dao.BaseDaoI;
import com.jxc.model.po.Goods;
import com.jxc.model.po.GoodsRepertory;
import com.jxc.model.vo.VGoodsRepertory;


@Repository("baseDao")
public class BaseDaoImpl<T> implements BaseDaoI<T> {

	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	protected Session getCurrentSession() {
		return sessionFactory.getCurrentSession();
	}

	public Serializable save(T o) {
		return this.getCurrentSession().save(o);
	}

	public void delete(T o) {
		this.getCurrentSession().delete(o);
	}

	public void update(T o) {
		try {
			this.getCurrentSession().update(o);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	public void saveOrUpdate(T o) {
		this.getCurrentSession().saveOrUpdate(o);
	}

	public List<T> find(String hql) {
		//getCurrentSession().clear();
		return this.getCurrentSession().createQuery(hql).list();
	}
	
//	public List findBySql(String hql) {
//		return this.getCurrentSession().createSQLQuery(hql).addScalar("ID").addScalar("GOODS_ID").addScalar("REPERTORY_ID").list();
//	}
	public List<Object[]> findBySql(String hql) {
		//清理缓存
		//getCurrentSession().clear();
		List<Object[]> list = this.getCurrentSession().createSQLQuery(hql).list();
		return list;
//		this.getCurrentSession().createSQLQuery(hql).addScalar("ID").addScalar("GOODS_ID").addScalar("REPERTORY_ID").list();
	}
	/**
	 * 清理缓存对象
	 * @return
	 */
	@Override
	public List<Object[]> findBySqlClean(String hql){
		//getCurrentSession().clear();
		List<Object[]> list = this.getCurrentSession().createSQLQuery(hql).list();
		return list;
		
	}
	/**
	 * 将对象从缓存中移除
	 * @param object
	 */
	@Override
	public void clean(){
		getCurrentSession().clear();
	}
	@Override
	public Session  getSession(){
		return getCurrentSession();
	}
	
	public List findList(String hql){
		return this.getCurrentSession().createQuery(hql).list();
	}
	public List<T> find(String hql, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return q.list();
	}

	public List<T> find(String hql, List<Object> param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return q.list();
	}

	public List<T> find(String hql, Object[] param, Integer page, Integer rows) {
		if (page == null || page < 1) {
			page = 1;
		}
		if (rows == null || rows < 1) {
			rows = 10;
		}
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}

	public List<T> find(String hql, List<Object> param, Integer page, Integer rows) {
		if (page == null || page < 1) {
			page = 1;
		}
		if (rows == null || rows < 1) {
			rows = 10;
		}
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}
	public List<Object[]> findSql(String hql, List<Object> param, Integer page, Integer rows) {
		if (page == null || page < 1) {
			page = 1;
		}
		if (rows == null || rows < 1) {
			rows = 10;
		}
		Query q = this.getCurrentSession().createSQLQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}

	public List<T> find(String hql,  Integer page, Integer rows) {
		if (page == null || page < 1) {
			page = 1;
		}
		if (rows == null || rows < 1) {
			rows = 10;
		}
		Query q = this.getCurrentSession().createQuery(hql);
		
		return q.setFirstResult((page - 1) * rows).setMaxResults(rows).list();
	}

	public T get(Class<T> c, Serializable id) {
		return (T) this.getCurrentSession().get(c, id);
	}

	public T get(String hql, Object[] param) {
		List<T> l = this.find(hql, param);
		if (l != null && l.size() > 0) {
			return l.get(0);
		} else {
			return null;
		}
	}

	public T get(String hql, List<Object> param) {
		List<T> l = this.find(hql, param);
		if (l != null && l.size() > 0) {
			return l.get(0);
		} else {
			return null;
		}
	}

	public Long count(String hql) {
		return (Long) this.getCurrentSession().createQuery(hql).uniqueResult();
	}
	public Integer count2(String hql) {
		return (Integer) this.getCurrentSession().createQuery(hql).uniqueResult();
	}
	public Double count1(String hql) {
		return (Double) this.getCurrentSession().createQuery(hql).uniqueResult();
	}

	public Long count(String hql, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return (Long) q.uniqueResult();
	}

	public BigInteger countSQL(String hql) {
		Query q = this.getCurrentSession().createSQLQuery(hql);
//		if (param != null && param.length > 0) {
//			for (int i = 0; i < param.length; i++) {
//				q.setParameter(i, param[i]);
//			}
//		}
		return (BigInteger) q.uniqueResult();
	}
	public BigDecimal countSQL2(String hql) {
		Query q = this.getCurrentSession().createSQLQuery(hql);
//		if (param != null && param.length > 0) {
//			for (int i = 0; i < param.length; i++) {
//				q.setParameter(i, param[i]);
//			}
//		}
		return  (BigDecimal) q.uniqueResult();
	}
	public Double countSQL1(String hql) {
		Query q = this.getCurrentSession().createSQLQuery(hql);
//		if (param != null && param.length > 0) {
//			for (int i = 0; i < param.length; i++) {
//				q.setParameter(i, param[i]);
//			}
//		}
		return (Double) q.uniqueResult();
	}
	public Long count(String hql, List<Object> param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return (Long) q.uniqueResult();
	}
	
	public Double countToDouble(String hql, List<Object> param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return (Double) q.uniqueResult();
	}

	public Integer executeHql(String hql) {
		return this.getCurrentSession().createQuery(hql).executeUpdate();
	}

	public Integer executeHql(String hql, Object[] param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.length > 0) {
			for (int i = 0; i < param.length; i++) {
				q.setParameter(i, param[i]);
			}
		}
		return q.executeUpdate();
	}

	public Integer executeHql(String hql, List<Object> param) {
		Query q = this.getCurrentSession().createQuery(hql);
		if (param != null && param.size() > 0) {
			for (int i = 0; i < param.size(); i++) {
				q.setParameter(i, param.get(i));
			}
		}
		return q.executeUpdate();
	}
	
	@Override
	public void executeSql(String sql) {
		this.getCurrentSession().createSQLQuery(sql);
//		this.getCurrentSession().createSQLQuery(hql).addScalar("ID").addScalar("GOODS_ID").addScalar("REPERTORY_ID").list();
	}

}
